package com.hertz.iris.core;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.media.RingtoneManager;

import com.hertz.iris.IrisApplication;
import com.hertz.iris.R;
import com.hertz.iris.dao.NormalAlarmDao;
import com.hertz.iris.pojo.NormalAlarm;
import com.hertz.iris.pojo.Ring;
import com.hertz.iris.util.Constants;
import com.hertz.iris.util.DateUtil;
import com.hertz.iris.util.Ln;
import com.hertz.iris.view.alert.NormalAlarmRec;

public class NormalAlarmService {
	@Inject
	public NormalAlarmService(){}

	public List<Ring> getSysRings(){
    	RingtoneManager ringtoneMgr = new RingtoneManager(IrisApplication.getInstance().getApplicationContext());
		ringtoneMgr.setType(RingtoneManager.TYPE_ALARM);
		Cursor alarmsCursor = ringtoneMgr.getCursor();
		List<Ring> rings = new ArrayList<Ring>(alarmsCursor.getCount() + 1);
		Ring r = new Ring();
		r.setName(IrisApplication.getInstance().getApplicationContext().getResources().getString(R.string.no_ring));
		r.setUri("");
		rings.add(r);
		if (alarmsCursor.moveToFirst()) {		    			
			do {
				Ring ring = new Ring();
				ring.setName(ringtoneMgr.getRingtone(alarmsCursor.getPosition()).getTitle(IrisApplication.getInstance().getApplicationContext()));
				ring.setUri(ringtoneMgr.getRingtoneUri(alarmsCursor.getPosition()).toString());
				rings.add(ring);
			}while(alarmsCursor.moveToNext());					
		}
		alarmsCursor.close();
        return rings;
    }
	
	public List<Ring> getSysNot(){
    	RingtoneManager ringtoneMgr = new RingtoneManager(IrisApplication.getInstance().getApplicationContext());
		ringtoneMgr.setType(RingtoneManager.TYPE_NOTIFICATION);
		Cursor alarmsCursor = ringtoneMgr.getCursor();
		List<Ring> rings = new ArrayList<Ring>(alarmsCursor.getCount() + 1);
		Ring r = new Ring();
		r.setName(IrisApplication.getInstance().getApplicationContext().getResources().getString(R.string.no_ring));
		r.setUri("");
		rings.add(r);
		if (alarmsCursor.moveToFirst()) {		    			
			do {
				Ring ring = new Ring();
				ring.setName(ringtoneMgr.getRingtone(alarmsCursor.getPosition()).getTitle(IrisApplication.getInstance().getApplicationContext()));
				ring.setUri(ringtoneMgr.getRingtoneUri(alarmsCursor.getPosition()).toString());
				rings.add(ring);
			}while(alarmsCursor.moveToNext());					
		}
		alarmsCursor.close();
        return rings;
    }
    
    public boolean saveAlarm(NormalAlarm normalAlarm){
    	long nextAlarmTime = DateUtil.getNextAlarm(normalAlarm);
    	DBService.getInstance().saveNormalAlarm(normalAlarm);
    	AlarmManager am = (AlarmManager) IrisApplication.getInstance().getSystemService(Context.ALARM_SERVICE);  
        Intent intent = new Intent(Constants.NORMAL_ALARM_ACTION);  
        intent.setClass(IrisApplication.getInstance().getApplicationContext(), NormalAlarmRec.class);  
        intent.putExtra(NormalAlarmDao.TABLENAME,normalAlarm);
        
        PendingIntent sender = PendingIntent.getBroadcast(IrisApplication.getInstance().getApplicationContext(), normalAlarm.getId().intValue(), intent, PendingIntent.FLAG_CANCEL_CURRENT);  
        am.set(AlarmManager.RTC_WAKEUP, nextAlarmTime, sender); 
    	Ln.d("iris", String.format("next alarm time %d",nextAlarmTime));
    	return true;
//    	DBService.getInstance(context).saveNormalAlarm(normalAlarm);
    }
    
    public List<NormalAlarm> loadAllNormalAlarm(){
    	return DBService.getInstance().loadAllNormalAlarm();
    }
    /*
     * update to db & resetAlarm
     */
    public void updateNormalAlarm(NormalAlarm normalAlarm){
    	DBService.getInstance().saveNormalAlarm(normalAlarm);
    	resetAlarmActive(normalAlarm);
    }
    
    public void deleteNormalAlarm(long id){
    	DBService.getInstance().deleteNormalAlarm(id);
    	cancelNormalAlarmAlert(id);
    }
    
    public void resetAlarmActive(NormalAlarm normalAlarm){
    	AlarmManager am = (AlarmManager) IrisApplication.getInstance().getApplicationContext().getSystemService(Context.ALARM_SERVICE);  
        Intent intent = new Intent(Constants.NORMAL_ALARM_ACTION);  
        intent.setClass(IrisApplication.getInstance().getApplicationContext(), NormalAlarmRec.class);  
        intent.putExtra(NormalAlarmDao.TABLENAME,normalAlarm);
        
        PendingIntent sender = PendingIntent.getBroadcast(IrisApplication.getInstance().getApplicationContext(), normalAlarm.getId().intValue(), intent, PendingIntent.FLAG_CANCEL_CURRENT);  
        if(normalAlarm.getIsActive()){
        	long nextAlarmTime = DateUtil.getNextAlarm(normalAlarm);
        	Ln.e("next alarm time : %d",nextAlarmTime);
        	am.set(AlarmManager.RTC_WAKEUP, nextAlarmTime, sender); 
        }else{
        	am.cancel(sender); 
        }
    }
    
    public void snooze(NormalAlarm normalAlarm){
    	long currentTIme = System.currentTimeMillis();
    	long nextAlarmTime = currentTIme + normalAlarm.getSnooze() * 60 * 1000;
    	
    	AlarmManager am = (AlarmManager) IrisApplication.getInstance().getSystemService(Context.ALARM_SERVICE);  
        Intent intent = new Intent(Constants.NORMAL_ALARM_ACTION);  
        intent.setClass(IrisApplication.getInstance().getApplicationContext(), NormalAlarmRec.class);  
        intent.putExtra(NormalAlarmDao.TABLENAME,normalAlarm);
        
        PendingIntent sender = PendingIntent.getBroadcast(IrisApplication.getInstance().getApplicationContext(), normalAlarm.getId().intValue(), intent, PendingIntent.FLAG_CANCEL_CURRENT);  
        am.set(AlarmManager.RTC_WAKEUP, nextAlarmTime, sender); 
    	Ln.d("iris", String.format("next snooze alarm time %d",nextAlarmTime));
    }
    public void cancelNormalAlarmAlert(long id){
    	AlarmManager am = (AlarmManager) IrisApplication.getInstance().getApplicationContext().getSystemService(Context.ALARM_SERVICE);  
        Intent intent = new Intent(Constants.NORMAL_ALARM_ACTION);  
        intent.setClass(IrisApplication.getInstance().getApplicationContext(), NormalAlarmRec.class);  
        
        PendingIntent sender = PendingIntent.getBroadcast(IrisApplication.getInstance().getApplicationContext(), Long.valueOf(id).intValue(), intent, PendingIntent.FLAG_CANCEL_CURRENT);  
        am.cancel(sender);
    }
}
